﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SH.STD.DataProvider;
using SH.STD.Model;

namespace SH.STD.Web.Achievement
{
    public partial class PromoteList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.SetTab(TabType.Achievement);

            if (!Page.IsPostBack)
            {
                var sql = @"
select a.ID,b.Name as AchievementName,a.TestZongdui,a.FeedBack,a.PromoteDate
from Promote a
join Achievement b
on a.AchievementID=b.ID
where a.status=1
order by a.ID";

                this.BindData(sql);
            }
        }

        private void BindData(string sql)
        {
            var dt = Cmn.DB.getDataTable(sql);

            this.rptPromotes.DataSource = dt;
            this.rptPromotes.DataBind();
            this.lblCnt1.Text = dt.Rows.Count.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var sql = @"
select a.ID,b.Name as AchievementName,a.TestZongdui,a.FeedBack,a.PromoteDate
from Promote a
join Achievement b
on a.AchievementID=b.ID
where a.status=1 and ";
            var where = String.Empty;
            if (!String.IsNullOrEmpty(this.txtPromoteStartDate.Text.Trim()))
            {
                where += "a.PromoteDate>='" + this.txtPromoteStartDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtPromoteEndDate.Text.Trim()))
            {
                where += "a.PromoteDate<='" + this.txtPromoteEndDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtTestZongdui.Text.Trim()))
            {
                where += "a.TestZongdui like '%" + this.txtTestZongdui.Text.Trim() + "%' and ";
            }
            if (!String.IsNullOrEmpty(this.txtMemberName.Text.Trim()))
            {
                where +=
@"a.ID in 
(   
    select a.ID 
    from Member a 
    join Employee b
    on a.EmployeeID=b.ID
    where a.TableID=5 and b.Name like '%" + this.txtMemberName.Text.Trim() + @"%'
) and ";
            }
            if (!String.IsNullOrEmpty(this.txtAchievementName.Text.Trim()))
            {
                where +=
@"b.ID in 
(   
    select ID
    from Achievement
    where status=1 and Name like '%" + this.txtAchievementName.Text.Trim() + @"%'
) and ";
            }
            sql = where == String.Empty ? sql.Substring(0, sql.LastIndexOf("and ")) : sql + " " + where.Substring(0, where.LastIndexOf("and "));

            this.BindData(sql);
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtAchievementName.Text = String.Empty;
            this.txtMemberName.Text = String.Empty;
            this.txtPromoteStartDate.Text = String.Empty;
            this.txtPromoteEndDate.Text = String.Empty;
            this.txtTestZongdui.Text = String.Empty;

            var sql = @"
select a.ID,b.Name as AchievementName,a.TestZongdui,a.FeedBack,a.PromoteDate
from Promote a
join Achievement b
on a.AchievementID=b.ID
where a.status=1
order by a.ID";

            this.BindData(sql);
        }
    }
}